package cn.school.web.controller.system;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.temporal.ChronoField;
import java.util.*;
import javax.servlet.http.HttpServletResponse;

import cn.school.common.constant.enums.OrderTypeEnum;
import cn.school.common.constant.enums.PayStatusEnum;
import cn.school.common.utils.DateUtils;
import cn.school.system.config.FriendsConfig;
import cn.school.system.model.resp.CountInfoRespDTO;
import cn.school.system.model.resp.GetIndexCountRespDTO;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import cn.school.common.annotation.Log;
import cn.school.common.core.controller.BaseController;
import cn.school.common.core.domain.AjaxResult;
import cn.school.common.enums.BusinessType;
import cn.school.system.domain.BiOrderInfo;
import cn.school.system.service.IBiOrderInfoService;
import cn.school.common.utils.poi.ExcelUtil;
import cn.school.common.core.page.TableDataInfo;

/**
 * 订单信息Controller
 *
 * @author ruoyi
 * @date 2023-12-15
 */
@RestController
@RequestMapping("/system/orderInfo")
public class BiOrderInfoController extends BaseController
{
    @Autowired
    private IBiOrderInfoService biOrderInfoService;
    @Autowired
    private FriendsConfig friendsConfig;

    /**
     * 查询订单信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:orderInfo:list')")
    @GetMapping("/list")
    public TableDataInfo list(BiOrderInfo biOrderInfo)
    {
        startPage();
        List<BiOrderInfo> list = biOrderInfoService.selectBiOrderInfoList(biOrderInfo);
        return getDataTable(list);
    }

    /**
     * 导出订单信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:orderInfo:export')")
    @Log(title = "订单信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BiOrderInfo biOrderInfo)
    {
        List<BiOrderInfo> list = biOrderInfoService.selectBiOrderInfoList(biOrderInfo);
        ExcelUtil<BiOrderInfo> util = new ExcelUtil<BiOrderInfo>(BiOrderInfo.class);
        util.exportExcel(response, list, "订单信息数据");
    }

    /**
     * 获取订单信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:orderInfo:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(biOrderInfoService.selectBiOrderInfoById(id));
    }

    /**
     * 新增订单信息
     */
    @PreAuthorize("@ss.hasPermi('system:orderInfo:add')")
    @Log(title = "订单信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BiOrderInfo biOrderInfo)
    {
        return toAjax(biOrderInfoService.insertBiOrderInfo(biOrderInfo));
    }

    /**
     * 修改订单信息
     */
    @PreAuthorize("@ss.hasPermi('system:orderInfo:edit')")
    @Log(title = "订单信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BiOrderInfo biOrderInfo)
    {
        return toAjax(biOrderInfoService.updateBiOrderInfo(biOrderInfo));
    }

    /**
     * 删除订单信息
     */
    @PreAuthorize("@ss.hasPermi('system:orderInfo:remove')")
    @Log(title = "订单信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(biOrderInfoService.deleteBiOrderInfoByIds(ids));
    }

    @GetMapping("/getEnums")
    public AjaxResult getEnums(){
        Map<String, List<Map<String,Object>>> map = new HashMap<>();
        map.put("orderType", OrderTypeEnum.getAllToList());
        map.put("payStatus", PayStatusEnum.getAllToList());
        return AjaxResult.success(map);
    }

    //统计
    @GetMapping("/getIndexCount")
    public AjaxResult getIndexCount(){
        //利润比例
        BigDecimal profitRate = BigDecimal.ONE.subtract(friendsConfig.getCommissionRate());

        //统计全部
        CountInfoRespDTO total = biOrderInfoService.indexCount(profitRate,PayStatusEnum.SUCCESS.getStatus(), null, null);
        if(total==null){
            total = new CountInfoRespDTO();
        }

        //统计当日
        LocalDateTime currentLDT = LocalDateTime.now();
        // 获取当前日期的开始时间（凌晨 00:00:00）
        LocalDateTime startOfDayLDT = currentLDT.with(LocalTime.MIN);

        // 获取当前日期的结束时间（23:59:59.999999999）
        LocalDateTime endOfDayLDT = currentLDT.with(LocalTime.MAX);

        Date begin = Date.from(startOfDayLDT.atZone(ZoneId.systemDefault()).toInstant());
        Date end = Date.from(endOfDayLDT.atZone(ZoneId.systemDefault()).toInstant());

        CountInfoRespDTO today = biOrderInfoService.indexCount(profitRate,PayStatusEnum.SUCCESS.getStatus(), begin, end);
        if(today==null){
            today = new CountInfoRespDTO();
        }

        GetIndexCountRespDTO res = new GetIndexCountRespDTO();
        res.setTotal(total);
        res.setToday(today);

        return AjaxResult.success(res);
    }
}
